package com.ds.home.database.dao;

import com.ds.common.JDSConstants;
import com.ds.common.logging.Log;
import com.ds.common.logging.LogFactory;
import com.ds.home.database.BaseHibernateDAO;
import com.ds.home.database.vo.SceneVO;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.hibernate.query.NativeQuery;

import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for SceneVO
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see SceneVO
 * @author MyEclipse Persistence Tools
 */
public class SceneDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(JDSConstants.CONFIG_KEY, SceneDAO.class);
	// property constants
	public static final String OBJVALUE = "objvalue";
	public static final String SENSORID = "sensorid";
	public static final String NAME = "name";



	public  List<SceneVO> loadByWhere(String where ){

		NativeQuery<SceneVO> query = this.getSession().createNativeQuery("select * from ha_scene "+where, SceneVO.class);
		List<SceneVO>  volist= query.getResultList();

		return volist;
	}

	public void update(SceneVO transientInstance) {
		log.debug("update SceneVO instance");
		try {
			
			getSession().update(transientInstance);
		
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}finally{
			close();
		}
	}

	public void delete(SceneVO persistentInstance) {
		log.debug("deleting SceneVO instance");
		try {
		
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}finally{
			close();
		}
	}

	public SceneVO findById(String id) {
		log.debug("getting SceneVO instance with id: " + id);
		SceneVO instance =null;
		try {
			 instance = (SceneVO) getSession().get(
					"com.ds.home.database.vo.SceneVO", id);
			
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}finally{
			close();
		}
		return instance;
	}

	public List<SceneVO>  findByExample(SceneVO instance) {
		log.debug("finding SceneVO instance by example");
		List results =null;
		try {
			 results = getSession()
					.createCriteria("com.ds.home.database.vo.SceneVO")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}finally{
			close();
		}
		return results;
	}

	public List<SceneVO>  findByProperty(String propertyName, Object value) {
		log.debug("finding SceneVO instance with property: " + propertyName
				+ ", value: " + value);
		List results =null;
		try {
			String queryString = "from SceneVO as model where model."
					+ propertyName + "= ?0";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			results= queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}finally{
			close();
		}
		return results;
	}

	public List<SceneVO>  findByObjvalue(Object objvalue) {
		return findByProperty(OBJVALUE, objvalue);
	}

	public List<SceneVO> findBySensorId(Object objvalue) {
		return findByProperty(SENSORID, objvalue);
	}

	public List<SceneVO>  findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List<SceneVO>  findAll() {
		log.debug("finding all SceneVO instances");
		List results =null;
		try {
			String queryString = "from SceneVO";
			Query queryObject = getSession().createQuery(queryString);
			results= queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			close();
		}
		return results;
	}



	public void insert(SceneVO instance) {
		log.debug("attaching dirty SceneVO instance");
		try {
		
			getSession().insert(instance);
			
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}finally{
			close();
		}
	}

}